package com.example.demo2.mapper;

import com.example.demo2.entity.*;
import jakarta.transaction.Transactional;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface EquipmentMapper {
    @Select("select * from equipment where eId=#{eId}")
    public Equipment findEquipmentByeId(@Param("eId") Integer eId);

    @Select("select * from equconnect where pjId = #{pjId}")
    public List<Equconnect> FindEquconnectByPjid(@Param("pjId") int pjId);

    @Select("select * from equconnect where eId = #{eId}")
    public List<Equconnect> FindEquconnectByEid(@Param("eId") int eId);

    @Select("select * from equipment where eId = #{eId}")
    public Equipment FindEquipmentById(@Param("eId") int eId);

    @Select("select * from solution where eId = #{eId}")
    public List<Solution> FindSolutionByEid(@Param("eId") int eId);

    @Insert("INSERT INTO orders (oDate, oStatus, uID) values (#{order.oDate},#{order.oStatus},#{order.uID})")
    @Options(useGeneratedKeys = true, keyProperty = "order.oId")
    public int AddSolutionIntoOrders(@Param("order") Orders order);

    @Insert("INSERT INTO orders_detail ( eId, oId, pjId,soId) values (#{orderDetail.eId},#{orderDetail.oId},#{orderDetail.pjId},#{orderDetail.soId})")
    public int AddOrderDetail(@Param("orderDetail") com.example.demo.entity.OrderDetail od);
    @Select("select eid from equconnect where pjId = #{pjid}")
    public List<Integer> findEquIdOfProject(@Param("pjid") int pjid);

    @Select("select * from equipment where eid = #{eId}")
    public Equipment findEquById(@Param("eId") int eid);

    @Delete("delete from equconnect where eId = #{eid}")
    public int deleteEquOfProject(@Param("eid") int eid);
    @Delete("delete from equipment where eId = #{eid}")
    public int deleteEquipmentOfProject(@Param("eid") int eid);




    @Transactional
    @DeleteProvider(type = SqlProvider.class, method = "deleteEquListOfProject")
    int deleteEquListOfProject(@Param("eIds") List<Integer> eIds);

    class SqlProvider {

        public String deleteEquListOfProject(Map<String, Object> params) {
            List<Integer> ids = (List<Integer>) params.get("eIds");
            StringBuilder sql = new StringBuilder();
            sql.append("DELETE FROM equconnect WHERE eId IN (");
            for (int i = 0; i < ids.size(); i++) {
                sql.append(ids.get(i));
                if (i < ids.size() - 1) {
                    sql.append(",");
                }
            }
            sql.append(")");
            return sql.toString();
        }
    }
    @Insert("INSERT INTO equipment (eName,eUnit) values (#{equipment.eName},#{equipment.eUnit})")
    @Options(useGeneratedKeys = true, keyProperty = "eId")
    public int addEquOfProject(@Param("equipment") Equipment equipment);

    @Insert("INSERT INTO equconnect (pjId, eId) values (#{equconnect.pjId}, #{equconnect.eId})")
    @Options(useGeneratedKeys = true, keyProperty = "cId")
    public int addEquConnect(@Param("equconnect") Equconnect equconnect);
}
